package com.player;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.player.dao.DAOLocator;
import com.player.dao.util.DummyDataBuilder;

@SuppressWarnings("serial")
public class RestServlet extends com.sun.jersey.spi.container.servlet.ServletContainer {

	private static final Logger log = Logger.getLogger(RestServlet.class.getName());

	@Override
	public void init() throws ServletException {
		super.init();
		// FIXME Developer mode - need better & save handling
		DAOLocator.init();
		createDummyData();
	}

	private void createDummyData() {
		log.warning("Creating dummy data...");
		DummyDataBuilder dummyDataBuilder = new DummyDataBuilder();
		log.fine("DEVMODE: cleaning database...");
		dummyDataBuilder.cleanAll();
		dummyDataBuilder.build(20);
		log.fine("DEVMODE: Creating dummy data... DONE");

	}

	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();

		// FIXME check if logged in and desired PATH is available to public or
		// only for logged in users (or use injected SecurityContext on
		// resources??)

		// if (user != null) {
		// if (isFirstLogin(user.getEmail())) {
		// resp.setContentType("text/html");
		// resp.getWriter().println("Personal Data");
		// } else {
		// resp.setContentType("text/plain");
		// resp.getWriter().println("Hello, " + user.getNickname());
		// }
		// } else {
		// resp.sendRedirect(userService
		// .createLoginURL("/login/registrationStatus")); // req.getRequestURI()
		// }

		super.doGet(req, resp);

	}

	private boolean isFirstLogin(String email) {
		// FIXME immpl
		return true;
	}
}
